﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        #region SText

        /// <summary>
        /// SText Dependency Property
        /// </summary>
        public static readonly DependencyProperty STextProperty =
            DependencyProperty.Register("SText", typeof(int), typeof(MainWindow),
                new FrameworkPropertyMetadata((int)5,
                    FrameworkPropertyMetadataOptions.None,
                    new PropertyChangedCallback(OnSTextChanged),
                    new CoerceValueCallback(CoerceSText)));

        /// <summary>
        /// Gets or sets the SText property. This dependency property 
        /// indicates ....
        /// </summary>
        public int SText
        {
            get { return (int)GetValue(STextProperty); }
            set { SetValue(STextProperty, value); }
        }

        /// <summary>
        /// Handles changes to the SText property.
        /// </summary>
        private static void OnSTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            MainWindow target = (MainWindow)d;
            int oldSText = (int)e.OldValue;
            int newSText = target.SText;
            target.OnSTextChanged(oldSText, newSText);
        }

        /// <summary>
        /// Provides derived classes an opportunity to handle changes to the SText property.
        /// </summary>
        protected virtual void OnSTextChanged(int oldSText, int newSText)
        {

        }

        /// <summary>
        /// Coerces the SText value.
        /// </summary>
        private static object CoerceSText(DependencyObject d, object value)
        {
            MainWindow target = (MainWindow)d;
            int desiredSText = (int)value;
            if (desiredSText > 9)
                desiredSText = 9;
            if (desiredSText < 0)
                desiredSText = 0;
            return desiredSText;
        }

        #endregion

        
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            DependencyPropertyDescriptor prop = DependencyPropertyDescriptor.FromProperty(ComboBox.TextProperty, typeof(ComboBox));
            prop.AddValueChanged(ss, (s, ee) =>
            {
                int r = 0;
                if (Int32.TryParse(ss.Text, out r))
                {
                        SText = r;
                }
            });
        }

        private void ss_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {

        }
    }
}
